<?php


namespace app\common\energyApi;


use PhpOffice\PhpSpreadsheet\Reader\Xls\MD5;
use think\Exception;

class JieShun
{
    public function __construct(){
        $this->url = "http://60.174.231.230:8091/api";  //
        $this->userName = "9999"; //商户编号
        $this->password = "123456"; //商户密钥
        $this->appId = "app01"; //
        $this->key = "01b470fd-67de-11ed-8c40-02004c4f4f50";
        $this->random = "333333";
        $this->downdoorcredential = '/door/downdoorcredential'; //新增人脸识别
        $this->facecheck = '/door/facecheck';//获取人脸检测结果
        $this->baseDevices = '/base/devices';//获取所有设备
        $this->baseaddperson = '/base/addperson';//新增人事资料
        $this->basedeleteperson = '/base/deleteperson';//删除人事资料
        $this->baseperson = '/base/person';  //查询人事资料
        $this->personlist = '/base/personlist';//查询所有人事资料
        $this->depts = '/base/depts';//查询组织架构
    }
    public function getToken(){
       $result =  $this->api_post('/internal/sign',[
           'userName'=>$this->userName,
           'password'=>$this->password,
       ]);
       $result = json_decode($result,true);
       if($result['code']==0){
            return $result['data'][0];
       }else{
           return false;
       }
    }
    /**
     *查组织架构
     */
    public function depts(){
        $data['pageIndex'] = 1;
        $data['pageSize'] = 50;
        $time = time();
        $header = $this->getSign($time);
        $result = $this->api_post($this->depts,$data,$header);
        $result = json_decode($result,true);
        if($result['code']==0){
            return $result;
        }else{
            return $result['msg'];
        }
    }
    /**
     *开通门禁服务
     */
    public function downdoorcredential($data=[]){
        $time = time();
        $header = $this->getSign($time);
        $result = $this->api_post($this->downdoorcredential,$data,$header);
        $result = json_decode($result,true);
        if($result['code']==0){
            return $result;
        }else{
            return $result['msg'];
        }
    }
    /**
     *校验图片
     */
    public function facecheck($data=[]){
        $time = time();
        $header = $this->getSign($time);
        $result = $this->api_post($this->facecheck,$data,$header);
        try {
            $result = json_decode($result,true);
            if($result['code']==0){
                return $result;
            }else{
               throw  new Exception($result['msg']);
            }
        }catch (Exception $exception){
            throw  new Exception($exception->getMessage());
        }
    }
    /**
     * 查询所有人事资料
     */
    public function personlist(){
       $data['pageIndex'] = 1;
       $data['pageSize'] = 50;
        $time = time();
        $header = $this->getSign($time);
        $result = $this->api_post($this->personlist,$data,$header);
        $result = json_decode($result,true);
        return $result;
    }
    /**
     *获取控制设备
     */
    public function getBaseDevices(){
        $time = time();
        $header = $this->getSign($time);
        $result = $this->api_post($this->baseDevices,[],$header);
        $result = json_decode($result,true);
        return $result;
    }
    /**
     * 新增人事资料
     */
    public function baseaddperson($data=[]){
        $time = time();
        $header = $this->getSign($time);
        $result = $this->api_post($this->baseaddperson,$data,$header);
        $result = json_decode($result,true);
        return $result;
    }
    /**
     * 查询人事资料
     */
    public function baseperson($data=[]){
        $time = time();
        $header = $this->getSign($time);
        $result = $this->api_post($this->baseperson,$data,$header);
        $result = json_decode($result,true);
        return $result;
    }
    /**
     * 删除人事资料
     */
    public function basedeleteperson($data=[]){
        $time = time();
        $header = $this->getSign($time);
        $result = $this->api_post($this->basedeleteperson,$data,$header);
        $result = json_decode($result,true);
        return $result;
    }
    /**
     * @param $timestamp获取签名
     */
    public function getSign($timestamp=''){
        $md5sign = 'random'.$this->random.'timestamp'.$timestamp.'key'.$this->key;
        $radom = strtoupper(md5($md5sign));
        $header = array(
            'appId: '.$this->appId,
            'random: '.$this->random,
            'v: 1.0',
            'sign: '.$radom,
            'timestamp:'.$timestamp,
            'Content-Type: application/json'
        );
        return $header;
    }
    //仅限于水电管理api使用
    public function api_post($url,$data = "",$header=[]){
        $curl = curl_init();
        $sendurl = $this->url.$url;
        curl_setopt_array($curl, array(
            CURLOPT_URL => $sendurl,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS =>isset($data)?json_encode($data):'',
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
            ),
        ));
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }

}